package com.study.talk.module.netty.handler;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.study.talk.module.entity.UserInfo;
import com.study.talk.module.netty.message.Attributes;
import com.study.talk.module.netty.message.Session;
import com.study.talk.module.netty.message.request.MessageRequest;
import com.study.talk.module.netty.message.response.MessageResponse;
import com.study.talk.module.netty.message.response.MessageSelfResponse;
import com.study.talk.module.service.FriendService;
import com.study.talk.module.service.UserInfoService;
import com.study.talk.module.service.UserService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 消息处理
 */

@Component
@Slf4j
@ChannelHandler.Sharable
public class MessageRequestHandler extends SimpleChannelInboundHandler<MessageRequest> {

    @Resource
    private UserService userService;

    @Resource
    private FriendService friendService;

    @Resource
    private UserInfoService userInfoService;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MessageRequest msg) throws Exception {
        log.info("消息发送事件,{}", msg);
        Session session = ctx.channel().attr(Attributes.SESSION).get();
        if (ObjectUtils.isNull(session)) return;
        if (!friendService.queryIsFriend(session.getUserId(), msg.getReceiverId())) {
            MessageSelfResponse messageSelfResponse = new MessageSelfResponse();
            messageSelfResponse.setSuccess(false);
            messageSelfResponse.setReceiverId(msg.getReceiverId());
            messageSelfResponse.setDateTime(msg.getDateTime());
            messageSelfResponse.setReason("对方不是你的好友！请添加好友后重试");
            userService.sendMessage(session.getUserId(),messageSelfResponse);
            return;
        }

        MessageSelfResponse messageSelfResponse = new MessageSelfResponse();
        messageSelfResponse.setSuccess(true);
        messageSelfResponse.setReceiverId(msg.getReceiverId());
        messageSelfResponse.setDateTime(msg.getDateTime());
        messageSelfResponse.setMessageType(msg.getMessageType());
        messageSelfResponse.setContent(msg.getContent());

        userService.sendMessage(session.getUserId(), messageSelfResponse);

        MessageResponse messageResponse = new MessageResponse();
        UserInfo userInfo = userInfoService.getById(session.getUserId());
        messageResponse.setMessageType(msg.getMessageType());
        messageResponse.setDateTime(msg.getDateTime());
        messageResponse.setUserId(session.getUserId());
        messageResponse.setAvatar(userInfo.getAvatar());
        messageResponse.setContent(msg.getContent());
        messageResponse.setUsername(userInfo.getUsername());
        messageResponse.setNickname(userInfo.getNickName());
        userService.sendMessage(msg.getReceiverId(), messageResponse);

    }
}
